// pages/shopping_mall/shopping_details/shopping_details.js
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: [{
        img: "/images/swiper1.jpg"
      }, {
        img: "/images/swiper2.jpg"
      },
      {
        img: "/images/swiper3.jpg"
      }
    ],
    infoArray:[],
    name: "商品",
    price: 100,
    add: 1,
    hidden: 0,
    add2: 1,
    hidden2: 0,
    add3: 1,
    hidden3: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  //console.log(options.type);
    switch (options.type) {
      case '日常用品':
        var type = "1";
        break;
      case "礼品券":
        var type = "2";
        break;
      case "地铁日票":
        var type = "3";
        break;
      default:
        var type = "4";
        break;
    }
    console.log(type);
    db.collection("Products").where({
        id:type
      })
      .get()
      .then(res => {
        this.setData({
          info: res.data
        })
        // console.log(this.data.info);
        // console.log(this.data.info.length);
        // console.log(this.data.info[0].product.length)
          for(let j=0,len2=this.data.info[0].product.length;j<len2;j++)
          {
            if(this.data.info[0].product[j].name==options.name)
            {
              this.data.infoArray.push(this.data.info[0].product[j])
            }
          }
        
        this.setData({
            infoArray:this.data.infoArray
        })
        console.log(this.data.infoArray);
      })
      .catch(err => {
        console.log(err);
      })

  },
  add: function (e) {
    this.setData({
      add: !this.data.add,
      hidden: !this.data.hidden
    })
  },
  add2: function (e) {
    this.setData({
      add2: !this.data.add2,
      hidden2: !this.data.hidden2
    })
  },
  add3: function (e) {
    this.setData({
      add3: !this.data.add3,
      hidden3: !this.data.hidden3
    })
  },

  toSuccess : function (e) {
      wx.navigateTo({
        url: '../success/msg_success',
        success:res =>{
          wx.setStorage({
            data: this.data.infoArray[0],
            key: 'info',
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})